* A17a and b : Year-by-year attrition dummy ex-post

clear
clear mata
clear matrix
set maxvar 20000
set more off

pwd
cap cd c(`pwd')

estimates clear

use "../dta/reduced-form-master.dta", clear

drop if add_sample_w2==1|add_sample_w5==1

* Creating exit dummy - if someone filled out all previous years but not this year.
** gen exit variable, replace = missing for all following years if person has attrited in year y

gen t1=0
replace t1=1 if originaltvillage==1

sort id wave
gen exit = 0
bys id : replace exit = 1 if wave != (wave[_n-1] + 1) & firstt_2009!=1
replace exit=0 if wave==0

bys id : replace exit = 1 if c_problem[_n+1]==1

** For folks added in 2009
bys id : replace exit = 1 if wave != (wave[_n-1] + 1) & wave>3 & firstt_2009==1

bys id : gen exited = sum(exit)
gen lag_exited=exited[_n-1]

replace exit = . if (exited & lag_exited)

** Now, running t3 with exit as the dependent variable

replace flyer_hyv=1 if flyer_hyv_exp==1

** Don't include flyer_exp in marketing assignment

local uniqinst "assigned_risk_ws_2008 discount_2008 rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv bdmperc_2009 fourbdmperc_2009 disc4game_2009 bdmperc_2010 fourbdmperc_2010 disc4game_2010 assigned_risk_ws_2010 assigned_video_test assigned_drought_flyer assigned_subsidies_flyer assigned_loan bdmperc_2011 fourbdmperc_2011 disc4game_2011 bdmperc_2012 fourbdmperc_2012 disc4game_2012 bdmperc_2013 fourbdmperc_2013 disc4game_2013"  /* sewaT peerT assigned_risk_ws_2011 assigned_risk_ws_2012 assigned_risk_ws_2013 mrkt_allnegative mrkt_poslang mrkt_posimg*/

foreach var of varlist `uniqinst' {
	replace `var' = 0 if `var'==.
	replace `var' = 0 if wave==0
	}

foreach var of varlist `uniqinst' {
	replace `var' = 0 if treat_year==0
}

log using ../res/ex-post-attrition, replace


/* OLS */


local col=1
	di "`table'"
	
	reg exit year2006-year2013  t1 firstt_2007 firstt_2009 lost_w3 resurveyed if firstt_2009==0 , r  cluster(villageno)
	test t1 firstt_2007 firstt_2009
	estimates store cT`table'C`col++'	
	predict ols_all_treat_v, xb
	
	reg exit `uniqinst' year2006-year2013  t1 firstt_2007 firstt_2009 lost_w3 resurveyed , r  cluster(villageno)
	test `uniqinst' t1 firstt_2007 firstt_2009
	estimates store cT`table'C`col++'	
	predict ols_all_treat_i, xb
	
local uniqinst "assigned_risk_ws_2008 discount_2008 rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv bdmperc_2009 fourbdmperc_2009 disc4game_2009 bdmperc_2010 fourbdmperc_2010 disc4game_2010 assigned_risk_ws_2010 assigned_video_test assigned_drought_flyer assigned_subsidies_flyer assigned_loan bdmperc_2011 fourbdmperc_2011 disc4game_2011 bdmperc_2012 fourbdmperc_2012 disc4game_2012 bdmperc_2013 fourbdmperc_2013 disc4game_2013"  /* sewaT peerT assigned_risk_ws_2011 assigned_risk_ws_2012 assigned_risk_ws_2013 mrkt_allnegative mrkt_poslang mrkt_posimg*/

/* PROBIT */

	probit exit year2006-year2013  t1 firstt_2007 firstt_2009 lost_w3 resurveyed if firstt_2009==0 , r  cluster(villageno)
	test t1 firstt_2007 firstt_2009
	estimates store cT`table'C`col++'	
	predict probit_all_treat_v, pr


	probit exit `uniqinst' year2006-year2013  t1 firstt_2007 firstt_2009 lost_w3 resurveyed , r  cluster(villageno)
	test `uniqinst' t1 firstt_2007 firstt_2009
	estimates store cT`table'C`col++'	
	predict probit_all_treat_i, pr


	qui estout cT`table'C* using "../out/exit-t3.csv", replace ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	mlabel("" "" "" "") ///
	label stats( N)


/* IV REGRESSIONS */
local col=1
	di "`table'"	

local uniqinst "assigned_risk_ws_2008 discount_2008 rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv bdmperc_2009 fourbdmperc_2009 disc4game_2009 bdmperc_2010 fourbdmperc_2010 disc4game_2010 assigned_risk_ws_2010 assigned_video_test assigned_drought_flyer assigned_subsidies_flyer assigned_loan bdmperc_2011 fourbdmperc_2011 disc4game_2011 bdmperc_2012 fourbdmperc_2012 disc4game_2012 bdmperc_2013 fourbdmperc_2013 disc4game_2013"  /* sewaT peerT assigned_risk_ws_2011 assigned_risk_ws_2012 assigned_risk_ws_2013 mrkt_allnegative mrkt_poslang mrkt_posimg*/

	* Village IV (No Fixed Effects) 
	ivreg2 exit (s_policy_units=treat_year) year2*  firstt_2007 lost_w3 resurveyed if firstt_2009==0, r cluster(villageno)
	test s_policy_units firstt_2007 
	estimates store cT`table'C`col++'
	predict ols_attrit_vill, xb 

	/* IND IV (No Fixed Effects) */
	ivreg2 exit (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed firstt_2009 , r  cluster(villageno)
	test s_policy_units firstt_2007 firstt_2009
	estimates store cT`table'C`col++'	
	predict ols_attrit_ind, xb
	
	qui estout cT`table'C* using "../out/exit-t3.csv", append ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (s_policy_units) prehead("`ovar'") ///
	mlabel("" "" "" "") ///
	label stats(cdf N)
	
local col=1
	di "`table'"	
	
	*Probits 
	ivprobit exit (s_policy_units=treat_year) year2*  firstt_2007 lost_w3 resurveyed if firstt_2009==0 , r  cluster(villageno)
	test s_policy_units firstt_2007 
	estimates store cT`table'C`col++' 

	predict attrit_vill, pr 
	
	ivprobit exit (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed firstt_2009 , r  cluster(villageno)
	test s_policy_units firstt_2007 firstt_2009
	estimates store cT`table'C`col++'
	
	predict attrit_ind, pr

	qui estout cT`table'C* using "../out/exit-t3.csv", append ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (s_policy_units) prehead("`ovar'") ///
	mlabel("" "" "" "") ///
	label stats(chi2 N)
	
la var ols_attrit_vill "Village level IV - OLS"
la var ols_attrit_ind "Individual level IV - OLS"
la var attrit_vill "Village level IV - probit"
la var attrit_ind "Ind level IV - probit"
la var ols_all_treat_v "Village-level reduced form - OLS"
la var ols_all_treat_i "Individual-level reduced form - OLS"
la var probit_all_treat_v "Village-level reduced form - probit"
la var probit_all_treat_v "Individual-level reduced form - probit"

foreach var of varlist ols_attrit_vill ols_attrit_ind attrit_vill attrit_ind ols_all_treat_v ols_all_treat_i probit_all_treat_v probit_all_treat_i {
sum `var'
local l`var': variable label `var'
hist `var', percent xtitle("predicted attrition rate") ytitle("percent") title("`l`var''")
graph save ../out/`var'.gph, replace
}

grc1leg ../out/ols_all_treat_v.gph ../out/ols_all_treat_i.gph ../out/probit_all_treat_v.gph ../out/probit_all_treat_i.gph ../out/ols_attrit_vill.gph ../out/ols_attrit_ind.gph ../out/attrit_vill.gph ../out/attrit_ind.gph , graphregion(color(white) lwidth(large)) plotregion(icolor(white)) 
graph export "../out/exit-attrition-expost.emf", replace

gen inv_attrit_v = 1/attrit_vill
gen inv_attrit_i = 1/attrit_ind
gen inv_ols_att_v = 1/ols_attrit_vill
gen inv_ols_att_i = 1/ols_attrit_ind
gen inv_ols_all_treat_v = 1/ols_all_treat_v
gen inv_probit_all_treat_v = 1/probit_all_treat_v
gen inv_ols_all_treat_i = 1/ols_all_treat_i
gen inv_probit_all_treat_i = 1/probit_all_treat_i


log close 

keep if ex_post_balp==1

local uniqinst "discount_2007 groupT muslimT hinduT video ppayT vframeT pframeT assigned_risk_ws_2008 discount_2008 rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv bdmperc_2009 fourbdmperc_2009 disc4game_2009 bdmperc_2010 fourbdmperc_2010 disc4game_2010 assigned_risk_ws_2010 assigned_video_test assigned_drought_flyer assigned_subsidies_flyer assigned_loan bdmperc_2011 fourbdmperc_2011 disc4game_2011 bdmperc_2012 fourbdmperc_2012 disc4game_2012 bdmperc_2013 fourbdmperc_2013 disc4game_2013"  /* sewaT peerT assigned_risk_ws_2011 assigned_risk_ws_2012 assigned_risk_ws_2013 mrkt_allnegative mrkt_poslang mrkt_posimg*/

foreach var of varlist `uniqinst' {
	replace `var' = 0 if `var'==.
	replace `var' = 0 if wave==0
	}

foreach var of varlist `uniqinst' {
	replace `var' = 0 if treat_year==0
}

drop if id==111246 | id==113474
local table = 1
local rastring "replace"
local outcomevars "w_real_sav w_real_lent w_real_borrow w_real_food_exp w_real_nonfood_exp w_real_durables_exp w_real_events_exp w_real_income w_real_gifts_out w_real_gifts_in food_sufficiency_child fladder_std outlook_std control_std w_real_cons"
foreach var of local outcomevars {
replace `var' = 0 if `var' == .
}

xtset id mkt_year

*a17a
foreach ovar of varlist `outcomevars' {
local col=1
	di "`ovar'"
	di "`table'"

	/* Village IV (No Fixed Effects) */
	ivreg2 `ovar' (s_policy_units=treat_year) year2*  firstt_2007 lost_w3 resurveyed [pw=inv_attrit_v] if firstt_2009==0, r cluster(villageno) 
	estimates store cT`table'C`col++'
	
	/* Village IV(FE/RE) */
	xtivreg2 `ovar' (s_policy_units=treat_year) year2*  firstt_2007 lost_w3 resurveyed [pw=inv_attrit_v] if firstt_2009==0, fe cluster(villageno)
	estimates store cT`table'C`col++'
	
	/* IND IV (No Fixed Effects) */
	ivreg2 `ovar' (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed  firstt_2009 [pw=inv_attrit_i] , r cluster(villageno) 
	estimates store cT`table'C`col++'
	
	/* IND IV (Individual FE/RE) */
	xtivreg2 `ovar' (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed firstt_2009 [pw=inv_attrit_i], fe cluster(villageno)
	estimates store cT`table'C`col++'	

	qui estout cT`table'C* using "../out/t3-invp-ivprobit.csv", ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (s_policy_units) prehead("`ovar'") ///
	mlabel("" "" "" "") ///
	`rastring' label stats(cdf N) 

local rastring "append"

}

*a17b

foreach ovar of varlist `outcomevars' {
local col=1
	di "`ovar'"
	di "`table'"

	/* Village IV (No Fixed Effects) */
	ivreg2 `ovar' (s_policy_units=treat_year) year2*  firstt_2007 lost_w3 resurveyed [pw=inv_probit_all_treat_v] if firstt_2009==0, r cluster(villageno) 
	estimates store cT`table'C`col++'
	
	/* Village IV(FE/RE) */
	xtivreg2 `ovar' (s_policy_units=treat_year) year2*  firstt_2007 lost_w3 resurveyed [pw=inv_probit_all_treat_v] if firstt_2009==0, fe cluster(villageno)
	estimates store cT`table'C`col++'
	
	/* IND IV (No Fixed Effects) */
	ivreg2 `ovar' (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed  firstt_2009 [pw=inv_probit_all_treat_i] , r cluster(villageno) 
	estimates store cT`table'C`col++'
	
	/* IND IV (Individual FE/RE) */
	xtivreg2 `ovar' (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed firstt_2009 [pw=inv_probit_all_treat_i], fe cluster(villageno)
	estimates store cT`table'C`col++'	

	qui estout cT`table'C* using "../out/t3-invp-probit.csv", ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (s_policy_units) prehead("`ovar'") ///
	mlabel("" "" "" "") ///
	`rastring' label stats(cdf N) 

local rastring "append"

}
